﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MovieLookup.WebServices;
using System.Net;

namespace MovieLookup
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void lookupMenuItem_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;

            if(lookupMenuItem.Text == "Lookup")
            {
                Movie[] movies = null;
                try
                {
                    Service ws = new Service();
                    //ws.Credentials = new NetworkCredential("uid", "pwd");
                    movies = ws.MovieLookup(titleTextBox.Text);
                }
                catch(Exception ex)
                {
                    Cursor.Current = Cursors.Default;
                    MessageBox.Show(ex.Message);
                }
                if(movices != null && movies.Length > 0)
                {
                    titleComboBox.Items.Clear();
                    foreach(Movie movie in movies)
                        titleComboBox.Items.Add(movie);
                    titleComboBox.Visible = true;
                    titleTextBox.Visible = false;
                    lookupMenuItem.Text = "Back";
                }
            }
            else
            {
                titleTextBox.Text = string.Empty;
                titleTextBox.Visible = true;
                titleComboBox.Visible = false;
                lookupMenuItem.Text = "Lookup";
            }
            Cursor.Current = Cursors.Default;
        }

        private void titleComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            Movie movie = titleComboBox.SelectedItem as Movie;
            resultTextBox.Text = string.Format("Title: {0}\r\nYear: {1}\r\n\r\nPlot: {2}", movie.Title, movie.Year, movie.Description);
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(movie.ImageUrl);
            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
            pictureBox.Image = new Bitmap(resp.GetResponseStream());
        }

        private void doneMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}